package com.gusto.engine.spring.parsers;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import com.gusto.engine.semsim.measures.impl.ComposedSimilarityParam;

@Deprecated
public class ParserComposedSimilarityParam extends AbstractBeanDefinitionParser {
	
	private static BeanDefinitionBuilder parseProperty(Element element, ParserContext parserContext) {
	      BeanDefinitionBuilder property = BeanDefinitionBuilder.rootBeanDefinition(String.class);
	      
	      //BeanDefinition bd = parserContext.getDelegate().parseBeanDefinitionElement(element).getBeanDefinition();
	      BeanDefinition bd = parserContext.getDelegate().parseCustomElement(element);
	      
	      property.addConstructorArgValue(bd);
	      return property;
	   }
	
	protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
		BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(ComposedSimilarityParam.class);
		
		if (element.hasAttribute("type")) {
			String type = element.getAttribute("type");
			beanDefinitionBuilder.addPropertyValue("type", type);
		}
		if (element.hasAttribute("weight")) {
			String weight = element.getAttribute("weight");
			beanDefinitionBuilder.addPropertyValue("weight", weight);
		}
		if (element.hasAttribute("similarity")) {
			String similarity = element.getAttribute("similarity");
			beanDefinitionBuilder.addPropertyReference("similarity", similarity);
		}
		if (element.hasAttribute("property")) {
			String property = element.getAttribute("property");
			beanDefinitionBuilder.addPropertyValue("property", property);
		}
		
		Element property = DomUtils.getChildElementByTagName(element, "property");
		if (property != null) {
			BeanDefinitionBuilder child = parseProperty(property, parserContext);
			beanDefinitionBuilder.addPropertyValue("property", child.getBeanDefinition());
		}
		
		return beanDefinitionBuilder.getBeanDefinition();
	}
}
